package com.icegame.client;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 南京雪糕网络科技有限公司<p>
 *
 * @author 18486
 * @since 2019/9/28 14:58
 */
public class FTPClient implements Runnable{

    private Socket clientSocket;

    public FTPClient(String addr, int port){
        try {
            clientSocket = new Socket(addr,port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void run() {

        try {
                String str = null;

                BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                DataOutputStream outToServer=new DataOutputStream(clientSocket.getOutputStream());

                str=inFromServer.readLine();

                System.out.println("S >>> C: " + str);



                str="USER test\n";
                System.out.println("C >>> S: " +str);
                outToServer.write(str.getBytes());
                outToServer.flush();

                str=inFromServer.readLine();
                System.out.println("S >>> C: "+str);

                str="PASS 14147\n";
                System.out.println("C >>> S: "+str);
                outToServer.write(str.getBytes());
                outToServer.flush();

                str=inFromServer.readLine();
                System.out.println("S >>> C: "+str);

                str="PROT\n";
                System.out.println("C >>> S: "+str);
                outToServer.write(str.getBytes());
                outToServer.flush();

                str="LIST\n";
                System.out.println("C >>> S: "+str);
                outToServer.write(str.getBytes());
                outToServer.flush();


                while(!"end".equals(str=inFromServer.readLine())){

                    System.out.println("S >>> C: "+str);
                }

                str="RETR\n";
                System.out.println("C>>>S: "+str);
                outToServer.write(str.getBytes());
                outToServer.write("test.txt\n".getBytes());
                outToServer.flush();

                str="PASV\n";
                System.out.println("C >>> S: "+str);
                outToServer.write(str.getBytes());
                outToServer.flush();

                str=inFromServer.readLine();
                System.out.println(str);
                String portNumber=str;
                int port=Integer.parseInt(portNumber);
                TakeFile takeit=new TakeFile(port);
                takeit.takeIt();

                str="QUIT\n";
                System.out.println("C >>> S: "+str);
                outToServer.write(str.getBytes());
                outToServer.flush();

                str=inFromServer.readLine();
                System.out.println("S >>> C: "+str);

                while((str=inFromServer.readLine()) != null){
                    str=inFromServer.readLine();
                    System.out.println("S >>> C: "+str);
                }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
